package com.fastmms.analysis;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class AnalysisBlockResult {
    private List<AnalysisBlock> analysisBlockList = new ArrayList<>(0);
    private int maxBlockSize = -1;
    private long startNo = -1;
    private long endNo = -1;

    private List<EntryNode> nodeList = new ArrayList<>(11);

    public List<AnalysisBlock> getAnalysisBlockList() {
        return analysisBlockList;
    }

    public void setAnalysisBlockList(List<AnalysisBlock> analysisBlockList) {
        this.analysisBlockList = analysisBlockList;
    }

    public int getMaxBlockSize() {
        return maxBlockSize;
    }

    public void setMaxBlockSize(int maxBlockSize) {
        this.maxBlockSize = maxBlockSize;
    }

    public long getStartNo() {
        return startNo;
    }

    public void setStartNo(long startNo) {
        this.startNo = startNo;
    }

    public long getEndNo() {
        return endNo;
    }

    public void setEndNo(long endNo) {
        this.endNo = endNo;
    }

    public void buildNode(){
        if(analysisBlockList == null){
            return;
        }
        nodeList.clear();
        int size = analysisBlockList.size() / 10;
        if(size == 0){
            for (int i = 0; i < analysisBlockList.size(); i++) {
                AnalysisBlock block = analysisBlockList.get(i);
                EntryNode node = new EntryNode();
                nodeList.add(node);
                node.setStartMsgNo(block.getMsgStartNo());
                node.setEndMsgNo(block.getMsgEndNo() );
                node.setStartIndex(i);
                node.setEndIndex(i);
            }
            return;
        }
        for (int i = 0; i < 10; i++) {
            List<AnalysisBlock> subList = analysisBlockList.stream().skip( i * size ).limit(size).collect(Collectors.toList());
            AnalysisBlock startBlock = subList.get(0);
            AnalysisBlock endBlock = subList.get(subList.size() - 1);
            EntryNode node = new EntryNode();
            nodeList.add(node);
            node.setStartMsgNo(startBlock.getMsgStartNo());
            node.setEndMsgNo(endBlock.getMsgEndNo() );
            node.setStartIndex(i * size);
            node.setEndIndex(i * size + size - 1);
        }
        if(analysisBlockList.size() % size != 0){
            List<AnalysisBlock> subList = analysisBlockList.stream().skip( 10 * size ).collect(Collectors.toList());
            AnalysisBlock startBlock = subList.get(0);
            AnalysisBlock endBlock = subList.get(subList.size() - 1);
            EntryNode node = new EntryNode();
            nodeList.add(node);
            node.setStartMsgNo(startBlock.getMsgStartNo());
            node.setEndMsgNo(endBlock.getMsgEndNo() );
            node.setStartIndex(10 * size);
            node.setEndIndex( analysisBlockList.size()- 1);
        }
    }


    public List<EntryNode> getNodeList() {
        return nodeList;
    }

    public class EntryNode {
        private long startMsgNo;
        private long endMsgNo;
        private int startIndex;
        private int endIndex;

        public long getStartMsgNo() {
            return startMsgNo;
        }

        public void setStartMsgNo(long startMsgNo) {
            this.startMsgNo = startMsgNo;
        }

        public long getEndMsgNo() {
            return endMsgNo;
        }

        public void setEndMsgNo(long endMsgNo) {
            this.endMsgNo = endMsgNo;
        }

        public int getStartIndex() {
            return startIndex;
        }

        public void setStartIndex(int startIndex) {
            this.startIndex = startIndex;
        }

        public int getEndIndex() {
            return endIndex;
        }

        public void setEndIndex(int endIndex) {
            this.endIndex = endIndex;
        }

       @Override
       public String toString() {
           return "EntryNode{" +
                   "startNo=" + startNo +
                   ", endNo=" + endNo +
                   ", startIndex=" + startIndex +
                   ", endIndex=" + endIndex +
                   '}';
       }
   }

}
